﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.osx {
    import flash.events.*;
    import flash.display.*;
    import flash.utils.*;
    import flash.external.*;

    public class MouseWheelEnabler {

        private static var initialised:Boolean = false;
        private static var currentItem:InteractiveObject;
        private static var browserMouseEvent:MouseEvent;
        private static var lastEventTime:uint = 0;
        public static var useRawValues:Boolean;
        public static var eventTimeout:Number = 50;

        public static function init(_arg1:Stage, _arg2:Boolean=false):void{
            if (isMacSafari()){
                return;
            };
            if (!initialised){
                initialised = true;
                registerListenerForMouseMove(_arg1);
                registerJS();
            };
            useRawValues = _arg2;
        }
        private static function isMacSafari():Boolean{
            var _local1:BrowserInfo;
            if (ExternalInterface.available){
                ExternalInterface.call(MouseWheelEnabler_JavaScript.CODE);
                _local1 = getBrowserInfo();
                if (((((_local1) && ((_local1.browser == BrowserInfo.SAFARI_AGENT)))) && ((_local1.platform == BrowserInfo.MAC_PLATFORM)))){
                    return (true);
                };
            };
            return (false);
        }
        private static function registerListenerForMouseMove(_arg1:Stage):void{
            var stage = _arg1;
            stage.addEventListener(MouseEvent.MOUSE_MOVE, function (_arg1:MouseEvent):void{
                currentItem = InteractiveObject(_arg1.target);
                browserMouseEvent = MouseEvent(_arg1);
            });
        }
        private static function registerJS():void{
            var id:String;
            if (ExternalInterface.available){
                id = ("mws_" + Math.floor((Math.random() * 1000000)));
                ExternalInterface.addCallback(id, function ():void{
                });
                ExternalInterface.call("mws.InitMouseWheelSupport", id);
                ExternalInterface.addCallback("externalMouseEvent", handleExternalMouseEvent);
            };
        }
        private static function handleExternalMouseEvent(_arg1:Number, _arg2:Number=-1):void{
            var _local3:Number;
            if (_arg2 == -1){
                _arg2 = _arg1;
            };
            var _local4:uint = getTimer();
            if (_local4 >= (eventTimeout + lastEventTime)){
                if (useRawValues){
                    _local3 = _arg1;
                } else {
                    _local3 = _arg2;
                };
                if (((currentItem) && (browserMouseEvent))){
                    currentItem.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_WHEEL, true, false, browserMouseEvent.localX, browserMouseEvent.localY, browserMouseEvent.relatedObject, browserMouseEvent.ctrlKey, browserMouseEvent.altKey, browserMouseEvent.shiftKey, browserMouseEvent.buttonDown, int(_local3)));
                };
                lastEventTime = _local4;
            };
        }
        public static function getBrowserInfo():BrowserInfo{
            var _local1:Object;
            var _local2:Object;
            var _local3:String;
            if (ExternalInterface.available){
                _local1 = ExternalInterface.call("mws.getBrowserInfo");
                _local2 = ExternalInterface.call("mws.getPlatformInfo");
                _local3 = ExternalInterface.call("mws.getAgentInfo");
                return (new BrowserInfo(_local1, _local2, _local3));
            };
            return (null);
        }

    }
}//package net.wooga.pets.osx 

class MouseWheelEnabler_JavaScript {

    public static const CODE:XML = <script><![CDATA[
		function()
		{
			// create unique namespace
			if(typeof mws == "undefined" || !mws)	
			{
				mws = {};
			}
			
			var userAgent = navigator.userAgent.toLowerCase();
			mws.agent = userAgent;
			mws.platform = 
			{
				win:/win/.test(userAgent),
				mac:/mac/.test(userAgent),
				other:!/win/.test(userAgent) && !/mac/.test(userAgent)
			};
			
			mws.vars = {};
			
			mws.browser = 
			{
				version: (userAgent.match(/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/) || [])[1],
				safari: /webkit/.test(userAgent) && !/chrome/.test(userAgent),
				opera: /opera/.test(userAgent),
				msie: /msie/.test(userAgent) && !/opera/.test(userAgent),
				mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
				chrome: /chrome/.test(userAgent)
			};
			
			// find the function we added
			mws.findSwf = function(id) 
			{
				var objects = document.getElementsByTagName("object");
				for(var i = 0; i < objects.length; i++)
				{
					if(typeof objects[i][id] != "undefined")
					{
						return objects[i];
					}
				}
				
				var embeds = document.getElementsByTagName("embed");
				
				for(var j = 0; j < embeds.length; j++)
				{
					if(typeof embeds[j][id] != "undefined")
					{
						return embeds[j];
					}
				}
					
				return null;
			}
			
			mws.usingWmode = function( swf )
			{
				if( typeof swf.getAttribute == "undefined" )
				{
					return false;
				}
				
				var wmode = swf.getAttribute( "wmode" );
				if( typeof wmode == "undefined" )
				{
					return false;
				}
				
				return true;
			}
			
			//Debug logging
			mws.log = function( message ) 
			{
				if( typeof console != "undefined" )
				{
					console.log( message );
				}
				else
				{
					//alert( message );
				}
			}
			
			mws.shouldAddHandler = function( swf )
			{
				if( !swf )
				{
					return false;
				}
				
				return true;
			}
			
			mws.getBrowserInfo = function()
			{//getBrowserObj
				return mws.browser;
			}//getBrowserObj
			
			mws.getAgentInfo = function()
			{//getAgentInfo
				return mws.agent;
			}//getAgentInfo
			
			mws.getPlatformInfo = function()
			{//getPlatformInfo
				return mws.platform;
			}//getPlatformInfo
			
			mws.addScrollListeners = function()
			{//addScrollListeners
				
				// install mouse listeners
				if(typeof window.addEventListener != 'undefined') 
				{
					window.addEventListener('DOMMouseScroll', _mousewheel, false);
				}
				
				window.onmousewheel = document.onmousewheel = _mousewheel;
				
			}//addScrollListeners
			
			mws.removeScrollListeners = function()
			{//removeScrollListeners
				// install mouse listeners
				if(typeof window.removeEventListener != 'undefined') 
				{
					window.removeEventListener('DOMMouseScroll', _mousewheel, false);
				}
				
				window.onmousewheel = document.onmousewheel = null;
			}//removeScrollListeners
			
			mws.InitMouseWheelSupport = function(id) 
			{//InitMouseWheelSupport
				//grab reference to the swf
				var swf = mws.findSwf(id);
				
				//see if we can add the mouse listeners
				var shouldAdd = mws.shouldAddHandler( swf );
				
				if( shouldAdd ) 
				{
					/// Mousewheel support
					_mousewheel = function(event) 
					{//Mouse Wheel
							
						//Cover for IE
						if (!event) event = window.event;
						
						var rawDelta = 0;
						var divisor = 1;
						var scaledDelta = 0;
						
						//Handle scaling the delta.
						//This is becoming less and less useful as more browser/hardware combos emerge.
						if(event.wheelDelta)	
						{//normal event
							rawDelta = event.wheelDelta;
							
							if(mws.browser.opera)
							{
								divisor = 12;
							}
							else if(mws.browser.safari && mws.browser.version.split(".")[0] >= 528)
							{
								divisor = 12;
							}
							else
							{
								divisor = 120;
							}
						}//normal event
						else if(event.detail)		
						{//special event
							rawDelta = -event.detail;
						}//special event
						else
						{//odd event
							//Unhandled event type (future browser graceful fail)
							rawDelta = 0;
							scaledDelta = 0;
							
							//alert('Odd Event');
						}//odd event
						
						if(Math.abs(rawDelta) >= divisor)
						{//divide
							scaledDelta = rawDelta/divisor;
						}//divide
						else
						{//don't scale
							scaledDelta = rawDelta;
						}//don't scale
						
						//Call into the swf to fire a mouse event
						swf.externalMouseEvent(rawDelta, scaledDelta);
						
						if(event.preventDefault)	
						{//Stop default action
							event.preventDefault();
						}//Stop default action
						else
						{//stop default action (IE)
							return false;
						}//stop default action (IE)
							
						return true;
					}//MouseWheel

					//set up listeners
					swf.onmouseover = mws.addScrollListeners;
					swf.onmouseout = mws.removeScrollListeners;
				}//Should Add
					
			}//InitMouseWheelSupport
			
		}
	]]></script>
    ;

    public function MouseWheelEnabler_JavaScript(){
    }
}
